﻿Imports Windows.Phone.Storage.SharedAccess
Imports System.Windows.Navigation

Namespace API.WeiXin
    Public Class AssociationUriMapper
        Inherits UriMapperBase

        Dim AppID As String

        Public Sub New(appid As String)
            Me.AppID = appid
        End Sub

        Public Overrides Function MapUri(uri As Uri) As Uri
            Dim tempUri = uri.ToString

            If tempUri.Contains("/FileTypeAssociation") Then
                Dim fileIDIndex = tempUri.IndexOf("fileToken=") + 10
                Dim fileID = tempUri.Substring(fileIDIndex)

                Dim incommingFileName = SharedStorageAccessManager.GetSharedFileName(fileID)

                Dim extensionIndex = incommingFileName.LastIndexOf(".") + 1
                Dim incommingFileType = incommingFileName.Substring(extensionIndex).ToLower()

                Select Case incommingFileType
                    Case AppID
                        Return New Uri("/WXEntryPage.xaml?fileToken=" + fileID, UriKind.Relative)
                    Case Else
                        Return New Uri("/MainPage.xaml", UriKind.Relative)
                End Select
            Else
                Return uri
            End If
        End Function
    End Class
End Namespace